<?php

namespace App\Http\Controllers\V1\Api;

use App\Http\Controllers\V1\Api\BaseController;
use App\Models\V1\News;
use App\Models\V1\OpinionClass;
use App\Models\V1\Preparatory;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TimedTaskController extends BaseController
{
    /**
     * 每天00:05:00改变过期筹备组 状态
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changePreparatoryState(Request $request)
    {

        $currentTime = now(); // 获取当前时间
        $expiredData = Preparatory::where('expire_time', '<', $currentTime)
            ->where('state', 1)
            ->update(['state' => 0]);
        return ajax_output(200, "成功", $expiredData);

    }

    public function getNew(Request $request)
    {
        $host = "https://news1.market.alicloudapi.com";
        $path = "/channelList";
        $method = "GET";
        $appcode = "4701ccadbace42989886988f036d4f58";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "";
        $bodys = "";
        $url = $host . $path;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        var_dump(curl_exec($curl));
    }


    public function getNewInfo(Request $request)
    {





        $host = "https://tstoutiao.market.alicloudapi.com";
        $path = "/toutiao/index";
        $method = "GET";
        $appcode = "4701ccadbace42989886988f036d4f58";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
//        var_dump(curl_exec($curl));
        $response = curl_exec($curl);

        if (curl_errno($curl)) {
            // 处理CURL错误
            echo 'Curl error: ' . curl_error($curl);
        } else {
            // 假设我们不需要HTTP头部，只处理响应体
            // 如果CURLOPT_HEADER被设置为true，我们需要从响应中分离出HTTP头部和响应体
            $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
            $header = substr($response, 0, $headerSize);
            $body = substr($response, $headerSize);

            // 解码JSON响应体
            $data = json_decode($body, true); // 第二个参数为true时，将返回数组而不是对象

            // 现在你可以按需要处理$data数组了
            if (json_last_error() === JSON_ERROR_NONE) {
                // 成功解码，处理数据
                $model = new News();
                $now = Carbon::now();
//                $yesterdayEnd = $now->copy()->subDay()->endOfDay();
                $yesterdayEnd = $now->copy()->subDays(3)->endOfDay();

                $model->where('created_at', '<', $yesterdayEnd)->delete();
                foreach ($data['data']['list'] as $item) {
                    // 在这里可以访问每个数组元素的数据，例如：
                    $model->create([
                        'title' => $item['title'],
                        'thumb' => $item['pic'],
                        'content' => $item['content'],
                        'source' => $item['src'],
                        'new_time' => $item['time']
                    ]);
                }
            } else {
                // 解码失败，处理错误
                echo "Error decoding JSON: " . json_last_error_msg();
            }


        }


    }
}
